package cgroups

import (
	"github.com/getlantern/deepcopy"
	"github.com/opencontainers/runtime-spec/specs-go"
)

// reference:https://github.com/containers/crun/blob/master/crun.1.md
func Convert(resources *specs.LinuxResources, toolName string) *specs.LinuxResources {
	if toolName == "crun" {
		res := new(specs.LinuxResources)
		_ = deepcopy.Copy(res, resources)
		cpuConvert(res.CPU)
		blkioConvert(res.BlockIO)
		return res
	}
	return resources
}

// CPU controller
// 只需要修改 shares
func cpuConvert(source *specs.LinuxCPU) {
	*source.Shares = ConvertCPUSharesToCgroupV2Value(*source.Shares)
}

func blkioConvert(source *specs.LinuxBlockIO) {
	*source.Weight = uint16(ConvertBlkIOToCgroupV2Value(*source.Weight))
	for _, device := range source.WeightDevice {
		*device.Weight = uint16(ConvertBlkIOToCgroupV2Value(*source.Weight))
	}
}
